<!-- 羊只体重添加 -->
<template>
  <el-button @click="beginAdd()" type="danger" style="margin-top: 10px;">+新增</el-button>
  <el-button @click="save()" type="danger" style="margin-top: 10px;">保存</el-button>
  <el-button @click="cancel()" type="danger" style="margin-top: 10px;">撤销</el-button>
  <el-button @click="delBegin()" type="danger" style="margin-top: 10px;">删除</el-button>
  <br>
  <div style="background-color: lightgrey; height: 10px;margin-top: 10px;"></div>

  <h3 style="font-weight: 600;margin-top: 20px;">基本信息</h3>
  <hr>
  <div style="margin-top: 20px;margin-left: 30px;">称重日期：<el-date-picker
        v-model="form.weightData"
        type="date"
        placeholder="Pick a date"
        :default-value="new Date()"
        style="width: 200px;"
      /></div>
      <br>
      <el-form-item label="羊只耳号:" style="margin-top: -10px;margin-left: 30px;">
        <el-select  placeholder="请选择羊只耳号" v-model="form.earNumber" style="width: 200px">
          <el-option v-for="item in EarData" :label="item.earNumber" :value="item.earNumber" />
        </el-select>
      </el-form-item>
  <br>
  <el-form-item label="称重类型:"  style="margin-top: -30px;margin-left: 30px;">
        <el-select  placeholder="请选择称重类型" v-model="form.weightType" style="width: 200px">
          <el-option v-for="item in WeightData" :label="item.weightTypeName" :value="item.weightType" />
        </el-select>
      </el-form-item>
  <br>
  <div style="margin-top: -30px">重量（公斤）：<el-input style="width: 200px;" v-model="form.weight" placeholder="请输入重量"></el-input></div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted } from 'vue';
import axios from 'axios';
import { useRouter, useRoute } from 'vue-router';
import { ElTable } from 'element-plus';
import { ElMessage, ElMessageBox } from 'element-plus';
const router = useRouter();
const route = useRoute();

const form=reactive({
  weightData:new Date(),
  earNumber:'',
  weight:'',
  weightType:'',
  createId:localStorage.getItem('aaa')
})
const EarData:any=ref([])
const getEarData=()=>{
  axios.get('http://localhost:5039/api/SleepBegin/SelectEar').then(res=>{
    console.log(res)
    EarData.value=res.data.item
  })
}
const WeightData:any=ref([])
const getWeightData=()=>{
  axios.get('http://localhost:5039/api/SleepWeight/GetSleepType').then(res=>{
    console.log(res)
    WeightData.value=res.data.data
  })
}

//添加
const save=()=>{
  axios.post('http://localhost:5039/api/SleepWeight/AddSleepWeight',form).then(res=>{
     console.log(res)
     if(res.data.code>0){
      ElMessage.success('添加成功')
     }else if(res.data.code==-1){
      ElMessage.error('羊耳号的该体重已经添加')
     }
     
  })
}
//撤销
const cancel=()=>{
  form.earNumber=''
  form.weightType=''
  form.weight=''
}
//新增
const beginAdd=()=>{
  axios.post('http://localhost:5039/api/SleepWeight/AddSleepWeight',form).then(res=>{
     console.log(res)
     if(res.data.code>0){
      ElMessage.success('添加成功')
      cancel()
     }
  })
}



onMounted(()=>{
  getEarData()
  getWeightData()
})
</script>